<?php

class CronicasAdminController extends \BaseController {

	public function __construct(){
		
		$roles=array(
				'redactor'=>4,
				'admin'=>1,
				'usuario'=>'2',
				'admin.torneos'=>3
		);
		
		//permisos
		$rolesCronicas=array($roles['admin.torneos'],$roles['admin']);
		
		$this->beforeFilter('roles:'.implode($rolesCronicas,'-'));
	}
	
	
	/**
	 * Display a listing of noticiasadmins
	 *
	 * @return Response
	 */
	public function index()
	{
		$cronicas = Cronica::all();
		
		return View::make('contents.admin.cronicas.index', compact('cronicas'));
	}

	/**
	 * Show the form for creating a new noticiasadmin
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('contents.admin.cronicas.create');
	}

	/**
	 * Store a newly created noticiasadmin in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), Cronica::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		$cronica = new Cronica();
		$cronica->fill($data);
		$cronica->user()->associate(Auth::user());
		$cronica->torneo()->associate(Torneo::find(Input::get('torneo')));
		
		
		if(Input::hasFile('imagen')){
			
			$file            = Input::file('imagen');
			$destinationPath = public_path().'/assets/img/cronicas/';
			$filename        = str_random(6) . '_' . $file->getClientOriginalName();
			$uploadSuccess   = $file->move($destinationPath, $filename);
			
			
			$cronica->imagen=$filename;
		}
		
		$cronica->save();
		

		return Redirect::route('admin.cronicas.index');
	}

	/**
	 * Display the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$cronica = Cronica::findOrFail($id);

		return View::make('contents.admin.cronicas.show', compact('cronica'));
	}

	/**
	 * Show the form for editing the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$cronica = Cronica::find($id);

		return View::make('contents.admin.cronicas.edit', compact('cronica'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$cronica = Cronica::findOrFail($id);
		
		if($cronica->torneo->id==Input::get('torneo')){
			$i=0;
			$equipos=Input::get('equipo');
			$puntuaciones=Input::get('puntuacion');
			foreach ($cronica->torneo->users as $user){ 
				$user->pivot->equipo=$equipos[$i];
				$user->pivot->puntuacion=$puntuaciones[$i];
				$user->pivot->save();
				++$i;
			}
		} else {
			$cronica->torneo()->associate(Torneo::find(Input::get('torneo')));
		}
		
		$validator = Validator::make($data = Input::all(), Cronica::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		$cronica->update($data);

		if(Input::hasFile('imagen')){
		
			$file            = Input::file('imagen');
			$destinationPath = public_path().'/assets/img/cronicas/';
			$filename        = str_random(6) . '_' . $file->getClientOriginalName();
			$uploadSuccess   = $file->move($destinationPath, $filename);
		
			File::delete($destinationPath.$cronica->imagen);
				
			$cronica->imagen=$filename;
			$cronica->save();
		}
		return Redirect::route('admin.cronicas.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		Cronica::destroy($id);

		return Redirect::route('admin.cronicas.index');
	}

}